package com.plian.system.controller.yth;

import com.alibaba.fastjson.JSON;
import com.plian.Tools.SmUtils;
import com.plian.Tools.StringUtil;
import com.plian.system.config.AppKeySign;
import com.plian.system.config.AppKeySignAspect;
import com.plian.system.enumeratea.YthDataTransferEnum;
import com.plian.system.enumeratea.YthExcelTransferEnum;
import com.plian.system.model.R;
import com.plian.system.model.token.PassToken;
import com.plian.system.properties.YthProperties;
import com.plian.system.service.sys.orgzation.IOrgService;
import com.plian.system.service.sys.user.ISysUserService;
import com.plian.system.vo.fr.bank.AppSignVO;
import com.plian.system.vo.yth.YthExcelInfoVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.function.BiFunction;
import java.util.function.Function;

@RestController
@RequestMapping("/api/yth/")
public class YthController {

    @Autowired
    private YthProperties ythProperties;

    @Autowired
    private ISysUserService sysUserService;

    @Autowired
    private IOrgService orgService;

    @Autowired
    private AppKeySignAspect appKeySignAspect;

    @PostMapping("/data-transfer")
    @AppKeySign
    @PassToken
    public R dataTransfer(@RequestBody AppSignVO appSignVO){
        if (appSignVO.getDataType() == null){
            return R.fail("dataType为空");
        }
        if (!ythProperties.getIsEnabled()){
            return R.fail("该功能未开启");
        }
        Function<String, String> function = YthDataTransferEnum.getFunction(appSignVO.getDataType());
        if (function == null){
            return R.fail(StringUtil.format("dataType={},该类型暂不支持！", appSignVO.getDataType()));
        }
        String data = SmUtils.decryptSm2(appSignVO.getData(), ythProperties.getGzwPrivateKey());
        return R.data(SmUtils.encryptSm2(function.apply(data), ythProperties.getGzwPublicKey()));
    }

    @PostMapping("/user/submit-list")
    @AppKeySign
    @PassToken
    public R userSubmitList(@RequestBody AppSignVO appSignVO){
        if (!ythProperties.getIsEnabled()){
            return R.fail("该功能未开启");
        }
        String data = SmUtils.decryptSm2(appSignVO.getData(), ythProperties.getGzwPrivateKey());
        return  R.data(SmUtils.encryptSm2(sysUserService.ythUserSubmitList(data), ythProperties.getGzwPublicKey()));
    }

    @PostMapping("/user/remove")
    @AppKeySign
    @PassToken
    public R userRemove(@RequestBody AppSignVO appSignVO){
        if (!ythProperties.getIsEnabled()){
            return R.fail("该功能未开启");
        }
        String data = SmUtils.decryptSm2(appSignVO.getData(), ythProperties.getGzwPrivateKey());
        return  R.data(SmUtils.encryptSm2(sysUserService.ythUserRemove(data), ythProperties.getGzwPublicKey()));
    }

    @PostMapping("/user/details")
    @AppKeySign
    @PassToken
    public R userDetails(@RequestBody AppSignVO appSignVO){
        if (!ythProperties.getIsEnabled()){
            return R.fail("该功能未开启");
        }
        String data = SmUtils.decryptSm2(appSignVO.getData(), ythProperties.getGzwPrivateKey());
        return  R.data(SmUtils.encryptSm2(sysUserService.ythUserDetails(data), ythProperties.getGzwPublicKey()));
    }

    @PostMapping("/org/submit-list")
    @AppKeySign
    @PassToken
    public R orgSubmitList(@RequestBody AppSignVO appSignVO){
        if (!ythProperties.getIsEnabled()){
            return R.fail("该功能未开启");
        }
        String data = SmUtils.decryptSm2(appSignVO.getData(), ythProperties.getGzwPrivateKey());
        return  R.data(SmUtils.encryptSm2(orgService.ythOrgSubmitList(data), ythProperties.getGzwPublicKey()));
    }

    @PostMapping("/org/remove")
    @AppKeySign
    @PassToken
    public R orgRemove(@RequestBody AppSignVO appSignVO){
        if (!ythProperties.getIsEnabled()){
            return R.fail("该功能未开启");
        }
        String data = SmUtils.decryptSm2(appSignVO.getData(), ythProperties.getGzwPrivateKey());
        return  R.data(SmUtils.encryptSm2(orgService.ythOrgRemove(data), ythProperties.getGzwPublicKey()));
    }

    @PostMapping("/org/details")
    @AppKeySign
    @PassToken
    public R orgDetails(@RequestBody AppSignVO appSignVO){
        if (!ythProperties.getIsEnabled()){
            return R.fail("该功能未开启");
        }
        String data = SmUtils.decryptSm2(appSignVO.getData(), ythProperties.getGzwPrivateKey());
        return  R.data(SmUtils.encryptSm2(orgService.ythOrgDetails(data), ythProperties.getGzwPublicKey()));
    }

    @PostMapping("/excel-transfer")
    @PassToken
    public R excelTransfer(@RequestParam String dataInfo, @RequestParam("file") MultipartFile multipartFile){
        AppSignVO appSignVO = JSON.parseObject(dataInfo, AppSignVO.class);
        String checkMsg = appKeySignAspect.checkAppSign(appSignVO);
        if (StringUtil.isNotBlank(checkMsg)){
            return R.fail(checkMsg);
        }
        if (appSignVO.getExcelType() == null){
            return R.fail("excelType为空");
        }
        if (!ythProperties.getIsEnabled()){
            return R.fail("该功能未开启");
        }
        BiFunction<YthExcelInfoVO, MultipartFile, String> function = YthExcelTransferEnum.getFunction(appSignVO.getExcelType());
        if (function == null){
            return R.fail(StringUtil.format("excelType={},该类型暂不支持！", appSignVO.getExcelType()));
        }
        String data = SmUtils.decryptSm2(appSignVO.getData(), ythProperties.getGzwPrivateKey());
        YthExcelInfoVO excelInfoVO = JSON.parseObject(data, YthExcelInfoVO.class);
        return R.data(SmUtils.encryptSm2(function.apply(excelInfoVO, multipartFile), ythProperties.getGzwPublicKey()));
    }
}
